package vn.egame.elockscreen.viewpager;

import vn.egame.elockscreen.ELockscreenPreferences;
import vn.egame.elockscreen.R;
import vn.egame.elockscreen.activity.LockScreenDefaultActivity;
import vn.egame.elockscreen.animation.Techniques;
import vn.egame.elockscreen.animation.YoYo;
import vn.egame.elockscreen.utils.Fonts;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FragmentPin extends BaseFragment {

	private Activity activity;
	private TextView btnCancel, btnDelete;
	private TextView tvMessage, tvState;
	private TextView btnPin0, btnPin1, btnPin2, btnPin3, btnPin4, btnPin5,
			btnPin6, btnPin7, btnPin8, btnPin9;
	private ImageView btnPinBlank1, btnPinBlank2, btnPinBlank3, btnPinBlank4;
	private ImageView[] btnPinBlank;
	private ImageView imgFailPin, imgBgPin;
	private RelativeLayout layoutPin;
	private LinearLayout llPinBlank;
	private int positionInput;
	private int timeInput = 0;

	private String correctPasscode;
	private StringBuilder inputPasscode;
	Animation animationFadeOut;

	public FragmentPin() {
	}

	@Override
	public void setArguments(Bundle args) {
		super.setArguments(args);

	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = getActivity();
		animationFadeOut = AnimationUtils.loadAnimation(
				activity.getApplicationContext(), R.anim.fade_out);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_pin_fragment, null);

		initView(view);

		ELockscreenPreferences.getInstance().initPreferences(
				activity.getApplicationContext());
		correctPasscode = ELockscreenPreferences.getInstance()
				.getValuePasscode();
		inputPasscode = new StringBuilder();
		positionInput = 0;

		Log.d("Kai", inputPasscode.length() + "");
		return view;
	}

	public void initView(View view) {

		layoutPin = (RelativeLayout) view.findViewById(R.id.layoutPin);
		llPinBlank = (LinearLayout) view.findViewById(R.id.llPinBlank);
		// layoutPin.setAlpha(0);

		imgBgPin = (ImageView) view.findViewById(R.id.imgBgPin);
		imgBgPin.setAlpha(0);

		tvState = (TextView) view.findViewById(R.id.tvState);
		setFont(tvState);

		btnCancel = (TextView) view.findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(buttonClickListener);
		setFont(btnCancel);

		tvMessage = (TextView) view.findViewById(R.id.tvMessage);
		setFont(tvMessage);

		imgFailPin = (ImageView) view.findViewById(R.id.imgFailPin);
		imgFailPin.setVisibility(View.GONE);

		btnDelete = (TextView) view.findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(buttonClickListener);
		setFont(btnDelete);

		btnPin0 = (TextView) view.findViewById(R.id.btnPin0);
		btnPin0.setOnClickListener(pinClickListener);
		setFont(btnPin0);

		btnPin1 = (TextView) view.findViewById(R.id.btnPin1);
		btnPin1.setOnClickListener(pinClickListener);
		setFont(btnPin1);

		btnPin2 = (TextView) view.findViewById(R.id.btnPin2);
		btnPin2.setOnClickListener(pinClickListener);
		setFont(btnPin2);

		btnPin3 = (TextView) view.findViewById(R.id.btnPin3);
		btnPin3.setOnClickListener(pinClickListener);
		setFont(btnPin3);

		btnPin4 = (TextView) view.findViewById(R.id.btnPin4);
		btnPin4.setOnClickListener(pinClickListener);
		setFont(btnPin4);

		btnPin5 = (TextView) view.findViewById(R.id.btnPin5);
		btnPin5.setOnClickListener(pinClickListener);
		setFont(btnPin5);

		btnPin6 = (TextView) view.findViewById(R.id.btnPin6);
		btnPin6.setOnClickListener(pinClickListener);
		setFont(btnPin6);

		btnPin7 = (TextView) view.findViewById(R.id.btnPin7);
		btnPin7.setOnClickListener(pinClickListener);
		setFont(btnPin7);

		btnPin8 = (TextView) view.findViewById(R.id.btnPin8);
		btnPin8.setOnClickListener(pinClickListener);
		setFont(btnPin8);

		btnPin9 = (TextView) view.findViewById(R.id.btnPin9);
		btnPin9.setOnClickListener(pinClickListener);
		setFont(btnPin9);

		btnPinBlank1 = (ImageView) view.findViewById(R.id.btnPinBlank1);
		btnPinBlank2 = (ImageView) view.findViewById(R.id.btnPinBlank2);
		btnPinBlank3 = (ImageView) view.findViewById(R.id.btnPinBlank3);
		btnPinBlank4 = (ImageView) view.findViewById(R.id.btnPinBlank4);

		btnPinBlank = new ImageView[] { btnPinBlank1, btnPinBlank2,
				btnPinBlank3, btnPinBlank4 };
	}

	private void setFont(TextView view) {
//		view.setTypeface(Fonts.getInstance().fontLight);
	}

	View.OnClickListener pinClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			if (timeInput < 5 && positionInput < 4) {
				if (v.getId() == R.id.btnPin0) {
					setInputPasscode(0);
				} else if (v.getId() == R.id.btnPin1) {
					setInputPasscode(1);
				} else if (v.getId() == R.id.btnPin2) {
					setInputPasscode(2);
				} else if (v.getId() == R.id.btnPin3) {
					setInputPasscode(3);
				} else if (v.getId() == R.id.btnPin4) {
					setInputPasscode(4);
				} else if (v.getId() == R.id.btnPin5) {
					setInputPasscode(5);
				} else if (v.getId() == R.id.btnPin6) {
					setInputPasscode(6);
				} else if (v.getId() == R.id.btnPin7) {
					setInputPasscode(7);
				} else if (v.getId() == R.id.btnPin8) {
					setInputPasscode(8);
				} else if (v.getId() == R.id.btnPin9) {
					setInputPasscode(9);
				}

				checkInput();
			}
		}
	};

	View.OnClickListener buttonClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnCancel) {
				// getmUnlockListener().onCancelUnlock();
				((LockScreenDefaultActivity) activity).onCancelUnlock();
				if (inputPasscode.length() > 0) {
					for (int i = 0; i < 4; i++) {
						btnPinBlank[i].setSelected(false);
					}
					positionInput = 0;
					inputPasscode = new StringBuilder();
				}
			} else if (v.getId() == R.id.btnDelete) {
				if (inputPasscode.length() > 0) {
					inputPasscode.deleteCharAt(inputPasscode.length() - 1);
					positionInput--;
					btnPinBlank[positionInput].setSelected(false);
				}

				Log.d("Kai", inputPasscode.toString());
			}
		}
	};

	private void setInputPasscode(int value) {
		if (inputPasscode.length() < 4) {
			inputPasscode.append(value);
			Log.d("Kai", inputPasscode.toString());
			btnPinBlank[positionInput].setSelected(true);
			positionInput++;
		}
	}

	private int timeWait = 30;
	Handler handler = new Handler();
	Runnable timeWaitRunnable = new Runnable() {
		public void run() {
			timeWait--;
			if (timeWait == 0) {
				handler.removeCallbacks(timeWaitRunnable);
				timeWait = 30;
				timeInput = 0;
				tvMessage.setText("");
			} else {
				tvMessage.setText("Please wait: " + timeWait);
				handler.postDelayed(timeWaitRunnable, 1000);
			}
		}
	};

	private void checkInput() {
		if (inputPasscode.length() == 4) {

			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (checkPasscode()) {
						((LockScreenDefaultActivity) activity).onUnlock();
						// getmUnlockListener().onUnlock();
					} else {
						inputPasscode = new StringBuilder();
						timeInput++;
						imgFailPin.setVisibility(View.VISIBLE);
						imgFailPin.startAnimation(animationFadeOut);
						YoYo.with(Techniques.Shake).playOn(llPinBlank);

						animationFadeOut
								.setAnimationListener(new AnimationListener() {

									@Override
									public void onAnimationStart(
											Animation animation) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onAnimationRepeat(
											Animation animation) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onAnimationEnd(
											Animation animation) {
										// TODO Auto-generated method stub
										imgFailPin.setVisibility(View.GONE);
										for (int i = 0; i < 4; i++) {
											btnPinBlank[i].setSelected(false);
										}
										positionInput = 0;

									}
								});
						if (timeInput >= 5) {
							handler.postDelayed(timeWaitRunnable, 1000);
							tvMessage.setText("Please wait: " + timeWait);
						}
						Log.d("Kai", "incorrect");
					}
				}
			}, 200);

		}
	}

	private boolean checkPasscode() {
		if (inputPasscode.length() == 4
				&& inputPasscode.toString().equals(correctPasscode.toString()))
			return true;
		return false;
	}

	public void setAlpha(float alpha) {
		imgBgPin.setAlpha(alpha);
	}

	// @Override
	// public void setUserVisibleHint(boolean isVisibleToUser) {
	// super.setUserVisibleHint(isVisibleToUser);
	//
	// // Make sure that we are currently visible
	// if (this.isVisible()) {
	// // If we are becoming invisible, then...
	// imgBgPin.setAlpha(1);
	// }
	// }

}
